package com.itheima.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.Pro;
import com.itheima.service.ProService;
import com.itheima.service.impl.ProServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/proServlet")
public class ProServlet extends HttpServlet {
    private ProService service=new ProServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有的省份
        List<Pro> list = service.findAll();
        //转换json格式字符串
        ObjectMapper obj=new ObjectMapper();
        String json = obj.writeValueAsString(list);

        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(json);
    }
}
